import {IElement} from './IElement';
import {JSJaCPacket} from './JSJaCPacket'
/**
 * A jabber/XMPP iq packet
 * @class Models the XMPP notion of an 'iq' packet
 * @extends JSJaCPacket
 */
export class JSJaCIQ extends JSJaCPacket {
    constructor() {
        super('iq')
        
    }
    /**
     * Some combined method to set 'to', 'type' and 'id' at once
     * @param {string} to the recepients JID
     * @param {string} type A XMPP compliant iq type (one of 'set', 'get', 'result' and 'error'
     * @param {string} id A packet ID
     * @return {JSJaCIQ} this
     */
    setIQ(to:string,type:string,id:string):JSJaCIQ {
      if (to)
        this.setTo(to);
      if (type)
        this.setType(type);
      if (id)
        this.setID(id);
      return this;
    };
    /**
     * Creates a 'query' child node with given XMLNS
     * @param {string} xmlns The namespace for the 'query' node
     * @return {Node} The query node
     * @see {@link http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247 | Node}
     */
    setQuery(xmlns?:string):IElement{
      let query:IElement;
      try {
        query = this.getDoc().createElementNS(xmlns,'query');
      } catch (e) {
        query = this.getDoc().createElement('query');
        query.setAttribute('xmlns',xmlns);
      }
      this.getNode().appendChild(query);
      return query;
    };
    
    /**
     * Gets the 'query' node of this packet
     * @return {Node} The query node
     * @see {@link http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247 | Node}
     */
    getQuery():IElement{
      let queryElements=this.getNode().getElementsByTagName('query')
      if(queryElements.length>0)
         return queryElements[0]
      else
         return null
    };
    /**
     * Gets the XMLNS of the query node contained within this packet
     * @return {string} The namespace of the query node
     */
    getQueryXMLNS():string {
      if (this.getQuery()) {
        return this.getQuery().getNamespaceURI() || this.getQuery().getAttribute('xmlns');
      } else {
        return null;
      }
    };
    /**
     * Creates an IQ reply with type set to 'result'. If given appends payload to first child if IQ. Payload maybe XML as string or a DOM element (or an array of such elements as well).
     * @param {Array<Element>|Array<string>} [payload] An optional payload to be appended.
     * @return {JSJaCIQ} An IQ reply packet
     */
    replyusedArray(payloadList:Array<IElement>):JSJaCIQ {
      let rIQ:JSJaCIQ =<JSJaCIQ> this.clone();
      rIQ.setTo(this.getFrom());
      rIQ.setFrom();
      rIQ.setType('result');
      payloadList.forEach((payload:string|IElement) =>{
          if(typeof payload =='string'){
            //rIQ.getChild().appendChild(rIQ.getDoc().loadXML(payload));
          } else {
              rIQ.getChild().appendChild(payload);
          }
      });
      return rIQ;

    }
    /**
     * Creates an IQ reply with type set to 'result'. If given appends payload to first child if IQ. Payload maybe XML as string or a DOM element (or an array of such elements as well).
     * @param {Element|string} [payload] An optional payload to be appended.
     * @return {JSJaCIQ} An IQ reply packet
     */
    reply(payload:IElement):JSJaCIQ {
      let rIQ:JSJaCIQ =<JSJaCIQ> this.clone();
      rIQ.setTo(this.getFrom());
      rIQ.setFrom();
      rIQ.setType('result');
      rIQ.getChild().appendChild(payload);
      return rIQ;
    };

    clone(): JSJaCPacket {
      let oPacket:JSJaCIQ= new JSJaCIQ();
      oPacket.setDoc(this.getDoc().clone())
      return oPacket;
    }
}